<?php
/**
 * 显示总的271分布
 * 5587员工,特殊处理
 */
apf_require_controller("Abstract");
apf_require_class("Bll_Jx_Setting");
apf_require_class("Bll_Jx_Plan");
apf_require_class("Bll_Jx_Relation");
apf_require_class("Bll_Hr_OutInterface");
class Grade_ShowGradeController extends AbstractController {

    public function handle_request() {
        //取出所有271提交者
        $this->setting_bll = new Bll_Jx_Setting();
        $this->plan_bll = new Bll_Jx_Plan();
        $this->rel_bll = new Bll_Jx_Relation();
        $last_month = $this->setting_bll->format_performance();
        $date = $last_month[0];

        $params['year'] = empty($params['year']) ? date('Y') : $params['year'];

        $year_kp = $this->setting_bll->get_performance_month($params['year']);
print_r($year_kp);
        $leader_list = $this->rel_bll->get_271_leader();
        $tmp_all_performance = $this->plan_bll->find_performance(array('serachMonth'=>$date));
        $all_performance = array();
        if(!empty($tmp_all_performance)){
            foreach($tmp_all_performance as $val){
                $all_performance[$val['userId']] = $val;
            }
        }
        //todo应该获取,所有在职员工,生成上下级关系
        $this->kq_user = $this->rel_bll->get_kp_user(-1);
        //print_r($kq_user);
        $oa_bll = new Bll_Hr_OutInterface();
        $all_user = $oa_bll->build_user_relation();
        //过滤,只统计正式员工,试用员工
        foreach($all_user as $uid =>$val){
            if($val['user_type']!=1 &&  $val['user_type']!=2){
                unset($all_user[$uid]);
            }
        }
        //debug
        //$last = $this->tree($all_user,'SHA008');

        $datalist = array();
        foreach($leader_list as $value){
            $this->level_uids = array();//init
            //echo $value['code'];
            $last = $this->tree($all_user,$value['code']);
            $datalist[$value['user_id']]['max'] = 0;
            foreach($this->level_uids as $val){
                if(empty($this->kq_user[$val]) || $this->kq_user[$val]['flag']==1 ){//免考,不统计人数
                    continue;
                }
                $last_score = empty($all_performance[$val]['adjustScore']) ? $all_performance[$val]['personScore'] : $all_performance[$val]['adjustScore'];
                $str = Const_JxStatus::check_score_stage($last_score);
                $datalist[$value['user_id']]['max'] += 1;
                $datalist[$value['user_id']][$str] +=1;
            }
        }
        //tp($datalist);

        $this->request->set_attribute('all_user', $all_user);
        $this->request->set_attribute('datalist', $datalist);
        $this->request->set_attribute('year_kp',$year_kp);
        $this->request->set_attribute('params',$params);
        return 'Grade_ShowGrade';
    }

    private function tree($table,$p_id,$is_allow=1) {
        $tree = array();
        foreach($table as $id => $row){
            if($row['superior_code']==$p_id){
                //tp($row);
                if(empty($this->kq_user[$row['user_id']]['is271Leader'])){//如果不是271,就往下
                    $tmp = $this->tree($table,$row['code'],$is_allow+1);
                    $row['i']=$is_allow;
                    if($tmp){
                        foreach($tmp as $v){
                            if(!empty($this->kq_user[$v['user_id']]['is271Leader'])){//如果自己的下级也是271,就不在往下,这里
                                continue;
                            }
                            $this->level_uids[]=$v['user_id'];
                        }
                        $row['children']=$tmp;
                    }else{
                        $this->level_uids[]=$row['user_id'];
                        $row['leaf'] = true;
                    }
                }else{
                    $this->level_uids[]=$row['user_id'];
                }
                unset($table[$id]);
                $tree[]=$row;
            }
        }
        return $tree;
    }
}